package itbour.onetouchshow.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.adapter.BaseRecyclerHolder;
import com.google.gson.Gson;
import com.lsjr.utils.NetUtils;
import com.three.share.SharePlatform;
import com.three.share.ThirdShareUtils;
import com.three.share.listener.ShareListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.BaseRefreshAdapter;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.bean.JsShareBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.listener.MyRyItemListener;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.utils.sharedpreference.PreferencesHelper;

/**
 * Created by guoh on 2018/6/26.
 * 功能描述：
 * 需要的参数：
 */
@SuppressLint("ValidFragment")
public class UserProductShareDialogFragment extends DialogFragment {

    @BindView(R.id.gvShare)
    RecyclerView gvShare;
    @BindView(R.id.btnCancel)
    ImageView btnCancel;
    @BindView(R.id.tv_change_link_style)
    TextView tvChangeLinkStyle;

    Unbinder unbinder;
    ChangeShareLinkStyleDialogFragment.DialogType mAppType;
    List<Integer> icons = new ArrayList<>();
    private JsShareBean jsShareBean;
    private ShareListener mShareListener;
    private View.OnClickListener mChangeClickListener;

    @SuppressLint("ValidFragment")
    public UserProductShareDialogFragment(ChangeShareLinkStyleDialogFragment.DialogType appType) {
        this.mAppType = appType;
    }

    public void setData(JsShareBean jsShareBean, ShareListener mShareListener, View.OnClickListener onClickListener) {
        this.jsShareBean = jsShareBean;
        this.mShareListener = mShareListener;
        this.mChangeClickListener = onClickListener;
    }

    public void updateTitleAndContent() {
        if (this.jsShareBean != null) {
            String shareTitle = null;
            String shareContent = null;
            if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMAPP)) {
                shareTitle = PreferencesHelper.getData(AppConst.KEY_APP_SHARE_TITLE);
                shareContent = PreferencesHelper.getData(AppConst.KEY_APP_SHARE_CONTENT);
            } else if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMVIDEO)) {
                shareTitle = PreferencesHelper.getData(AppConst.KEY_CUSTOM_SHARE_TITLE);
                shareContent = PreferencesHelper.getData(AppConst.KEY_CUSTOM_SHARE_CONTENT);
            }
            if (!TextUtils.isEmpty(shareTitle)) {
                jsShareBean.setTitle(shareTitle);
            } else {
                HomeDetailslBean basicInfo = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);
                if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMAPP)) {
                    if (basicInfo != null && basicInfo.getAppControl() != null) {
                        jsShareBean.setTitle(basicInfo.getAppControl().getVideoShareTitle());
                    }
                } else if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMVIDEO)) {
                    if (basicInfo != null && basicInfo.getAppControl() != null) {
                        jsShareBean.setTitle(basicInfo.getAppControl().getCustomVideoShareTitle());
                    }
                }


            }
            if (!TextUtils.isEmpty(shareContent)) {
                jsShareBean.setContent(shareContent);
            } else {
                HomeDetailslBean basicInfo = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);
                if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMAPP)) {
                    if (basicInfo != null && basicInfo.getAppControl() != null) {
                        jsShareBean.setContent(basicInfo.getAppControl().getVideoShareMsg());
                    }
                } else if (mAppType.equals(ChangeShareLinkStyleDialogFragment.DialogType.TYPEOFCUSTOMVIDEO)) {
                    if (basicInfo != null && basicInfo.getAppControl() != null) {
                        jsShareBean.setContent(basicInfo.getAppControl().getCustomVideoShareMsg());
                    }
                }
            }


        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fr_user_product_share_dialog, null);
        unbinder = ButterKnife.bind(this, view);

        initView();
        return view;
    }

    private void initView() {
        tvChangeLinkStyle.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        if (mChangeClickListener != null) {
            tvChangeLinkStyle.setOnClickListener(mChangeClickListener);
        }
        List<String> titles = new ArrayList<>();
        titles.add("微信");
        titles.add("朋友圈");
        titles.add("QQ");
        titles.add("微博");
        icons.add(R.mipmap.icon_video_share_wechat);
        icons.add(R.mipmap.icon_video_share_wechat_moment);
        icons.add(R.mipmap.icon_video_share_qq);
        icons.add(R.mipmap.icon_video_share_weibo);
        gvShare.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), titles.size()));
        GvShareAdapter gvShareAdapter = new GvShareAdapter(getActivity().getApplication(), titles, R.layout.item_share_dialog);
        gvShare.setAdapter(gvShareAdapter);

        gvShareAdapter.setOnItemClickListener(new MyRyItemListener<Integer>() {
            @Override
            public void onItemSelect(Integer o) {
                if (!NetUtils.isConnected(UIUtils.getContext())) {
                    T_.showCustomToast("请检查网络");
                    dismiss();
                    return;
                }

                switch (o) {
                    /**
                     *  因为图片路径加载失败问题
                     *
                     * 倒是分享失败
                     */
                    case 0:
                        ThirdShareUtils.initialize().shareMedia(getActivity(), SharePlatform.WX, jsShareBean.getTitle(), jsShareBean.getContent(), jsShareBean.getUrl(), jsShareBean.getThumb(), mShareListener);
                        break;
                    case 1:
                        ThirdShareUtils.initialize().shareMedia(getActivity(), SharePlatform.WX_TIMELINE, jsShareBean.getTitle(), jsShareBean.getContent(), jsShareBean.getUrl(), jsShareBean.getThumb(), mShareListener);
                        break;
                    case 2:
                        ThirdShareUtils.initialize().shareMedia(getActivity(), SharePlatform.QQ, jsShareBean.getTitle(), jsShareBean.getContent(), jsShareBean.getUrl(), jsShareBean.getThumb(), mShareListener);
                        break;
                    case 3:
                        ThirdShareUtils.initialize().shareMedia(getActivity(), SharePlatform.WEIBO, jsShareBean.getTitle(), jsShareBean.getContent(), jsShareBean.getUrl(), jsShareBean.getThumb(), mShareListener);
                        break;
                    default:
                        break;
                }
                dismiss();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void onShow(Activity activity) {
        if (!isAdded()) {
            show(activity.getFragmentManager().beginTransaction(), "UserProductShareDialogFragment");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //底部动画
        window.getAttributes().windowAnimations = R.style.shareDialogStyle;
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public class GvShareAdapter extends BaseRefreshAdapter<String> {

        private List<String> mdata;

        public GvShareAdapter(Context context, List<String> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
            this.mdata = datas;
        }

        @Override
        protected void convert(BaseRecyclerHolder var1, String var2, int var3) {
            TextView textView = var1.getView(R.id.id_tv_title);
            ImageView imageView = var1.getView(R.id.id_tg_icon);
            imageView.setImageResource(icons.get(var3));
            ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
            layoutParams.width = UIUtils.WH()[0] / mdata.size();
            textView.setText(var2);
            var1.getView(R.id.ly_root).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemSelect(var3);
                    }
                }
            });
        }
    }
}
